package com.wsz.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wsz.commonutils.R;
import com.wsz.commonutils.ordervo.CourseUtils;
import com.wsz.eduservice.pojo.Course;
import com.wsz.eduservice.pojo.frontvo.CourseWebVo;
import com.wsz.eduservice.pojo.vo.*;
import com.wsz.eduservice.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@Api(tags = "课程管理")
@RestController
@RequestMapping("/eduservice/coures")
public class EduCourseController {
    @Autowired
    private CourseService courseService;

    @ApiOperation("条件查询发布课程信息课程列表")
    @PostMapping({"/pagePublishCourse/{current}/{limit}"})
    public R getpagePublishCourse(@ApiParam(name ="current",value = "页码数",required = true)@PathVariable long current,
                                @ApiParam(name = "limit",value = "每页记录数",required = true)@PathVariable long limit,
                                @ApiParam(name="PublishCourseQuery ",value="查询条件") @RequestBody(required = false) PublishCourseQuery publishCourseQuery) {

        String title = publishCourseQuery.getTitle();
        String teacherName = publishCourseQuery.getTeacherName();
        Integer lessonNum = publishCourseQuery.getLessonNum();
        String subjectLevelOne = publishCourseQuery.getSubjectLevelOne();
        String subjectLevelTwo = publishCourseQuery.getSubjectLevelTwo();


        Page<CoursePublic> pagey = new Page<>(current,limit);

        Page<CoursePublic> pagex = courseService.getCoursePublicPage(pagey,title,teacherName,lessonNum,subjectLevelOne,subjectLevelTwo);

        List<CoursePublic> records = pagex.getRecords();
        long total = pagex.getTotal();
        return R.ok().data("rescords",records).data("total",total);
    }
    @ApiOperation("查询所有发布课程列表")
    @GetMapping({"/publishCourseAll"})
    public R getPublishCourseAll() {
        List<CoursePublic> list = courseService.getCoursePublicAll();
        return R.ok().data("list",list);
    }

    @ApiOperation("查询所有课程列表")
    @GetMapping({"/getListCourse"})
    public R getListCourse() {
        List<Course> list = courseService.list(null);
        return R.ok().data("list",list);
    }

    @ApiOperation("条件查询课程列表")
    @PostMapping({"/getPageCourse/{current}/{limit}"})
    public R getPageCourse(@ApiParam(name ="current",value = "页码数",required = true)@PathVariable long current,
        @ApiParam(name = "limit",value = "每页记录数",required = true)@PathVariable long limit,
        @ApiParam(name="courseQuery",value="查询条件") @RequestBody(required = false) CourseQuery courseQuery) {
            String title = courseQuery.getTitle();
            Integer lessonNum = courseQuery.getLessonNum();
            BigDecimal price = courseQuery.getPrice();
            String status = courseQuery.getStatus();
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();

            if(StringUtils.isNotBlank(title)){
                queryWrapper.like("title",title);
            }
            if(lessonNum!= null){
                queryWrapper.eq("lesson_num",lessonNum);
            }
            if(price != null){
                queryWrapper.like("price",price);
            }
            if(StringUtils.isNotBlank(status)){
                queryWrapper.eq("status",status);
            }

            Page<Course> pagey = new Page<>(current,limit);

            IPage<Course> page = courseService.page(pagey, queryWrapper);

        List<Course> records = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("records",records).data("total",total);
    }


    @ApiOperation("添加课程信息")
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@ApiParam(name = "CourseInfoVo",value = "课程信息") @RequestBody CourseInfoVo courseInfoVo){
        String id = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    @ApiOperation("通过课程id查询课程基本信息")
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@ApiParam(name = "courseId",value = "课程id")@PathVariable String courseId) {
           CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    @ApiOperation("修改课程信息")
    @PostMapping("/updateCouresInfo")
    public R updateCouresInfo(@ApiParam(name="CouresInfo",value = "修改的课程信息")
                                  @RequestBody CourseInfoVo courseInfoVo) {
            courseService.updaCouresInfo(courseInfoVo);
        return R.ok();
    }

    @ApiOperation("根据课程Id查询课程确认信息")
    @GetMapping("/getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable String courseId){
       CoursePublic coursePublic = courseService.PublishCourseInfo(courseId);
        return R.ok().data("publishCourse",coursePublic);
    }

    @ApiOperation("课程最终发布")
    @PostMapping("/publishCourse/{id}")
    public R publicCourse(@PathVariable String id) {
        Course course = new Course();
        course.setId(id);
        course.setStatus("Normal");
        boolean b = courseService.updateById(course);
        if(b) return R.ok();
        else return R.error().message("发布失败");
    }

    @ApiOperation("根据课程Id删除课程")
    @DeleteMapping("/{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        courseService.removeByCourseId(courseId);
        return R.ok();
    }

    @ApiOperation("用户买课程通过课程Id获取课程信息")
    @PostMapping("/getCourseInfoOrder/{courseId}")
    public CourseUtils getCourseInfoOrder(@PathVariable String courseId) {
        CourseWebVo baseCourseId = courseService.getBaseCourseId(courseId);
        CourseUtils courseUtils = new CourseUtils();
        BeanUtils.copyProperties(baseCourseId,courseUtils);
        return courseUtils;
    }

}
